package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.AliyunOssUtil;
import com.doubi.AliyunSmsUtil;
import com.doubi.common.config.jwt.JwtUitl;
import com.doubi.constants.SystemConstants;
import com.doubi.model.sys.TAdver;
import com.doubi.model.sys.THotel;
import com.doubi.model.sys.TShortMessage;
import com.doubi.model.sys.TSysUser;
import com.doubi.modelvo.sys.AdverVo;
import com.doubi.modelvo.sys.HotelVo;
import com.doubi.modelvo.sys.ServiceProviderVo;
import com.doubi.modelvo.sys.TSysUserVo;
import com.doubi.utils.DataUtil;
import com.doubi.utils.DateTimeUtil;
import com.doubi.utils.JsonUtils;
import com.doubi.vo.AppUiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

@Service
@Slf4j
public class ApiUserService {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private JwtUitl jwtUitl;

    /**
     * MrDoubi
     * 2020年3月10日 10:27:00
     *
     * @param account  账户
     * @param password 密码
     * @return model
     */
    public AppUiModel<TSysUserVo> login(String account, String password) {
        if (DataUtil.isEmpty(account)) {
            return new AppUiModel<>(400, "请输入账号！");
        } else if (DataUtil.isEmpty(password)) {
            return new AppUiModel<>(400, "请输入密码！");
        }
        QueryWrapper<TSysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);
        TSysUser sysUser = sysUserService.getOne(wrapper);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(400, "账户不存在！");
        }
        if (DataUtil.isEmpty(sysUser.getPassword())) {
            return new AppUiModel<>(402, "账户不存在！");
        } else if (!sysUser.getPassword().equals(password)) {
            return new AppUiModel<>(400, "密码错误！");
        } else {
            TSysUserVo sysUserVo = new TSysUserVo();
            BeanUtils.copyProperties(sysUser, sysUserVo);
            sysUserVo.setToken(jwtUitl.generateToken(sysUser.getOid()));
            return new AppUiModel<>(sysUserVo);
        }
    }

    @Autowired
    private AliyunOssUtil aliyunOssUtil;

    /**
     * MrDoubi
     * 2020年3月10日 11:07:27
     * 验证码登录
     *
     * @param account 账户
     * @param code    验证码
     * @return model
     */
    public AppUiModel<TSysUserVo> saveCodelogin(String account, String code, String weixinid, String qqid, String weiboid, String name, String head) {
        if (DataUtil.isEmpty(account)) {
            return new AppUiModel<>(400, "请输入账号！");
        } else if (DataUtil.isEmpty(code)) {
            return new AppUiModel<>(400, "请输入验证码！");
        }
        TShortMessage shortMessage = shortMessageService.getBeanByMinute(account);
        if (DataUtil.isEmpty(shortMessage)) {
            return new AppUiModel<>(400, "验证码错误！");
        }
        if (!shortMessage.getCode().equals(code)) {
            return new AppUiModel<>(400, "验证码错误！");
        } else {
            shortMessageService.updateBeanStatus(account, code);
            TSysUser sysUser = sysUserService.getTSysUserByAccount(account);
            if (DataUtil.isEmpty(sysUser)) {//新用户注册
                sysUser = new TSysUser();
                sysUser.setUserType(SystemConstants.UserType.USER);
                sysUser.setAccount(account);
                sysUser.setName(DataUtil.isNotEmpty(name) ? name : "新用户");
                if(DataUtil.isNotEmpty(head)){
                    sysUser.setHead(aliyunOssUtil.upload(head));
                }
                sysUser.setCreateTime(new Date());
                sysUser.setLastLoginTime(new Date());
                if (DataUtil.isNotEmpty(weixinid)) {
                    sysUser.setWeixinid(weixinid);
                }
                if (DataUtil.isNotEmpty(qqid)) {
                    sysUser.setQqid(qqid);
                }
                if (DataUtil.isNotEmpty(weiboid)) {
                    sysUser.setWeiboid(weiboid);
                }
                sysUser.setBalance(BigDecimal.ZERO);
                sysUserService.save(sysUser);
                //return AppUiModel.error("账户不存在！");
            }
            if (DataUtil.isNotEmpty(weixinid)) {
                sysUser.setWeixinid(weixinid);
            }
            if (DataUtil.isNotEmpty(qqid)) {
                sysUser.setQqid(qqid);
            }
            if (DataUtil.isNotEmpty(weiboid)) {
                sysUser.setWeiboid(weiboid);
            }
            sysUserService.updateById(sysUser);
            TSysUserVo sysUserVo = new TSysUserVo();
            BeanUtils.copyProperties(sysUser, sysUserVo);
            sysUserVo.setToken(jwtUitl.generateToken(sysUser.getOid()));
            return new AppUiModel<>(sysUserVo);
        }
    }

    @Autowired
    private ShortMessageService shortMessageService;


    @Autowired
    private AliyunSmsUtil aliyunSmsUtil;

    /**
     * MrDoubi
     * 2020年3月10日 10:58:48
     * 发送验证吗
     *
     * @param account 账号
     * @param userIp  用户ip
     * @return model
     */
    public AppUiModel<String> addVerificationCode(String account, String userIp) {
        if (DataUtil.isEmpty(account)) {
            return new AppUiModel<>(400, "请填写手机号!");
        }
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        if (aliyunSmsUtil.sendSms(account, code)) {
            return shortMessageService.addVerificationCode(account, code, userIp);
        } else {
            return new AppUiModel<>(400, "验证码发送失败!");
        }

    }

    /**
     * MrDoubi
     * 2020年3月10日 11:36:10
     * 忘记密码
     *
     * @param account  账户
     * @param password 密码
     * @param code     验证码
     * @return model
     */
    public AppUiModel<String> updateForgetPassword(String account, String password, String code) {
        if (DataUtil.isEmpty(account)) {
            return new AppUiModel<>(400, "请输入账号!");
        } else if (DataUtil.isEmpty(password)) {
            return new AppUiModel<>(400, "请输入密码!");
        } else if (DataUtil.isEmpty(code)) {
            return new AppUiModel<>(400, "请输入验证码!");
        }
        TSysUser sysUser = sysUserService.getTSysUserByAccount(account);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(400, "账户不存在!");
        }
        TShortMessage shortMessage = shortMessageService.getBeanByMinute(account);
        if (DataUtil.isEmpty(shortMessage)) {
            return new AppUiModel<>(400, "验证码错误!");
        }
        if (!shortMessage.getCode().equals(code)) {
            return new AppUiModel<>(400, "验证码错误!");
        } else {
            sysUserService.updateUiPwd(sysUser, password);
            shortMessageService.updateBeanStatus(account, code);
            return new AppUiModel<>(200, "密码修改成功!");
        }
    }

    /**
     * MrDoubi
     * 2020年3月10日 11:53:49
     * 微信登录
     *
     * @param weixinid 微信id
     * @return model
     */
    public AppUiModel<TSysUserVo> weixinLogin(String weixinid) {
        if (DataUtil.isEmpty(weixinid)) {
            return new AppUiModel<>(400, "未授权微信!");
        }
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weixinid", weixinid);
        TSysUser sysUser = sysUserService.getOne(queryWrapper);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(402, "请绑定手机号!");
        }
        TSysUserVo sysUserVo = new TSysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        sysUserVo.setToken(jwtUitl.generateToken(sysUser.getOid()));
        return new AppUiModel<>(sysUserVo);
    }

    /**
     * MrDoubi
     * 2020年3月10日 11:53:49
     * qq登录
     *
     * @param qqid qqid
     * @return model
     */
    public AppUiModel<TSysUserVo> qqLogin(String qqid) {
        if (DataUtil.isEmpty(qqid)) {
            return new AppUiModel<>(400, "未授权qq!");
        }
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("qqid", qqid);
        TSysUser sysUser = sysUserService.getOne(queryWrapper);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(402, "请绑定手机号!");
        }
        TSysUserVo sysUserVo = new TSysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        sysUserVo.setToken(jwtUitl.generateToken(sysUser.getOid()));
        return new AppUiModel<>(sysUserVo);
    }

    /**
     * MrDoubi
     * 2020年3月10日 11:53:49
     * 微博登录
     *
     * @param weiboid 微博id
     * @return model
     */
    public AppUiModel<TSysUserVo> weiboLogin(String weiboid) {
        if (DataUtil.isEmpty(weiboid)) {
            return new AppUiModel<>(400, "未授权微博!");
        }
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weiboid", weiboid);
        TSysUser sysUser = sysUserService.getOne(queryWrapper);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(402, "请绑定手机号!");
        }
        TSysUserVo sysUserVo = new TSysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        sysUserVo.setToken(jwtUitl.generateToken(sysUser.getOid()));
        return new AppUiModel<>(sysUserVo);
    }

    /**
     * MrDoubi
     * 2020年3月16日 14:25:58
     * 绑定位置
     *
     * @param userOid  用户oid
     * @param province 省市区
     * @param city     省市区
     * @param area     省市区
     * @return model
     */
    public AppUiModel<String> updateBindUserLocation(String userOid, String province, String city, String area) {
        if (DataUtil.isEmpty(userOid)) {
            return new AppUiModel<>(401);
        }
        TSysUser sysUser = sysUserService.getById(userOid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setProvince(province);
        sysUser.setCity(city);
        sysUser.setArea(area);
        sysUserService.updateById(sysUser);
        return new AppUiModel<>(200);
    }


    /**
     * MrDoubi
     * 2020年3月10日 14:05:19
     * 通过唯一标识获取用户信息
     *
     * @param oid 唯一标识
     * @return model
     */
    public AppUiModel<TSysUserVo> updateGetUserBean(String oid) {
        log.info("oid:" + oid);
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setLastLoginTime(new Date());
        sysUserService.updateById(sysUser);
        TSysUserVo sysUserVo = new TSysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        return new AppUiModel<>(sysUserVo);
    }

    /**
     * MrDoubi
     * 2020年3月10日 14:25:36
     * 修改用户头像
     *
     * @param oid    oid
     * @param avatar 头像
     * @return model
     */
    public AppUiModel<String> updateUserAvatar(String oid, String avatar) {
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        } else if (DataUtil.isEmpty(avatar)) {
            return new AppUiModel<>(400, "请上传头像!");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setHead(avatar);
        sysUserService.updateById(sysUser);
        return new AppUiModel<>(200, "修改成功!");
    }


    /**
     * MrDoubi
     * 2020年3月10日 14:25:36
     * 修改用户昵称
     *
     * @param oid  oid
     * @param name 昵称
     * @return model
     */
    public AppUiModel<String> updateUserName(String oid, String name) {
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        } else if (DataUtil.isEmpty(name)) {
            return new AppUiModel<>(400, "请填写昵称");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setName(name);
        sysUserService.updateById(sysUser);
        return new AppUiModel<>(200, "修改成功");
    }

    /**
     * MrDoubi
     * 2020年3月10日 14:25:36
     * 修改用户性别
     *
     * @param oid oid
     * @param sex 性别
     * @return model
     */
    public AppUiModel<String> updateUserSex(String oid, Integer sex) {
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        } else if (DataUtil.isEmpty(sex)) {
            return new AppUiModel<>(400, "请选择性别！");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setSex(sex);
        sysUserService.updateById(sysUser);
        return new AppUiModel<>(200, "修改成功！");
    }

    /**
     * MrDoubi
     * 2020年3月10日 14:25:36
     * 修改用户生日
     *
     * @param oid      oid
     * @param birthday 生日
     * @return model
     */
    public AppUiModel<String> updateUserDate(String oid, String birthday) {
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        } else if (DataUtil.isEmpty(birthday)) {
            return new AppUiModel<>(400, "请选择性别!");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setBirthday(DateTimeUtil.strToDate(birthday, "yyyy-MM-dd"));
        sysUserService.updateById(sysUser);
        return new AppUiModel<>(200, "修改成功!");
    }

    /**
     * MrDoubi
     * 2020年3月10日 14:25:36
     * 修改用户密码
     *
     * @param oid      oid
     * @param password 密码
     * @return model
     */
    public AppUiModel<String> updateUserPassword(String oid, String password) {
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        } else if (DataUtil.isEmpty(password)) {
            return new AppUiModel<>(400, "请输入密码!");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        sysUser.setPassword(password);
        sysUserService.updateById(sysUser);
        return new AppUiModel<>(200, "修改成功!");
    }


    /**
     * MrDoubi
     * 2020年3月16日 13:59:27
     * 修改手机号
     *
     * @param oid     用户唯一标识
     * @param account 账户
     * @param code    验证码
     * @return model
     */
    public AppUiModel<String> updateUserAccount(String oid, String account, String code) {
        if (DataUtil.isEmpty(oid)) {
            return new AppUiModel<>(401);
        } else if (DataUtil.isEmpty(account)) {
            return new AppUiModel<>(400, "请输入手机号!");
        } else if (DataUtil.isEmpty(code)) {
            return new AppUiModel<>(400, "请输入验证码!");
        }
        QueryWrapper<TSysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);
        TSysUser accountUser = sysUserService.getOne(wrapper);
        if (DataUtil.isNotEmpty(accountUser)) {
            return new AppUiModel<>(400, "此手机号已经被注册过!");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        TShortMessage shortMessage = shortMessageService.getBeanByMinute(account);
        if (DataUtil.isEmpty(shortMessage)) {
            return new AppUiModel<>(400, "验证码错误!");
        }
        if (!shortMessage.getCode().equals(code)) {
            return new AppUiModel<>(400, "验证码错误!");
        } else {
            sysUser.setAccount(account);
            shortMessageService.updateBeanStatus(account, code);
            sysUserService.updateById(sysUser);
            return new AppUiModel<>(200, "修改成功!");
        }
    }

    /**
     * MrDoubi
     * 2020年3月16日 13:44:47
     * 查询账户是否有密码
     *
     * @param userOid 用户oid
     * @return model
     */
    public AppUiModel<Integer> isHavePassword(String userOid) {
        if (DataUtil.isEmpty(userOid)) {
            return new AppUiModel<>(401);
        }
        TSysUser sysUser = sysUserService.getById(userOid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        if (DataUtil.isEmpty(sysUser.getPassword())) {
            return new AppUiModel<>(200, "获取成功!", 0);
        } else {
            return new AppUiModel<>(200, "获取成功!", 1);
        }
    }

    /**
     * MrDoubi
     * 2020年3月18日 15:42:02
     * 获取我的页面
     *
     * @param userOid 用户oid
     * @return model
     */
    public AppUiModel<Map<String, Object>> getMyPage(String userOid) {
        if (DataUtil.isEmpty(userOid)) {
            return new AppUiModel<>(401);
        }
        TSysUser sysUser = sysUserService.getById(userOid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppUiModel<>(401);
        }
        //用户对象
        TSysUserVo sysUserVo = new TSysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        Map<String, Object> map = new HashMap<>();
        map.put("sysUser", sysUserVo);
        //map.put("readCount", orderMessageService.getCountByRead(userOid));
        //map.put("pending", orderInfoService.getCountByUserTypeOne(userOid, SystemConstants.OrderTypeOne.PENDING));
        //map.put("processing", orderInfoService.getCountByUserTypeOne(userOid, SystemConstants.OrderTypeOne.PROCESSING));
        //map.put("comment", orderInfoService.getCountByUserTypeOne(userOid, SystemConstants.OrderTypeOne.COMMENT));
        //map.put("aftersale", orderInfoService.getCountByUserTypeOne(userOid, SystemConstants.OrderTypeOne.AFTERSALE));
        return new AppUiModel<>(map);
    }

    @Autowired
    private AdverService adverService;

    /**
     * MrDoubi
     * 2020年3月18日 15:42:02
     * 获取首页
     *
     * @return model
     */
    public AppUiModel<Map<String, Object>> getHome() {
        Map<String, Object> map = new HashMap<>();
        //广告
        QueryWrapper<TAdver> adverWrapper = new QueryWrapper<>();
        adverWrapper.eq("position", 1);
        adverWrapper.orderByDesc("sort_num");
        map.put("adver", JsonUtils.copyList(adverService.list(adverWrapper), AdverVo.class));
        //酒店
        QueryWrapper<THotel> hotelQueryWrapper = new QueryWrapper<>();
        hotelQueryWrapper.last(" order by is_hot desc,praise desc limit 3");
        //map.put("hotel", JsonUtils.copyList(hotelService.list(hotelQueryWrapper), HotelVo.class));
        //2.摄影师3.摄像4.化妆 5.主持人
        //map.put("photograph", JsonUtils.copyList(serviceProviderService.getHotList(SystemConstants.ServiceUser.PHOTOGRAPH), ServiceProviderVo.class));
        //map.put("video", JsonUtils.copyList(serviceProviderService.getHotList(SystemConstants.ServiceUser.VIDEO), ServiceProviderVo.class));
        //map.put("makeup", JsonUtils.copyList(serviceProviderService.getHotList(SystemConstants.ServiceUser.MAKEUP), ServiceProviderVo.class));
        //map.put("preside", JsonUtils.copyList(serviceProviderService.getHotList(SystemConstants.ServiceUser.PRESIDE), ServiceProviderVo.class));
        return new AppUiModel<>(map);
    }

}
